﻿// 项目：	Guan.CMS.Web - Guan.CMS
// 文件名：	ProductsController.cs
// 
// 修改时间：2013-03-09 21:56
// 修改人：	Cooclsee

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using AutoMapper;
using Guan.CMS.Data.Repositories;
using Guan.CMS.Model.Entities;
using Guan.CMS.Web.Helpers;
using Guan.CMS.Web.ViewModels.Product;
using PagedList;

namespace Guan.CMS.Web.Controllers
{
    public class ProductsController : Controller
    {
        private readonly IProductRepository _productRepository;
        private readonly IProductCategoryRepository _productCategoryRepository;

        public ProductsController(IProductRepository productRepository,
                                  IProductCategoryRepository productCategoryRepository)
        {
            _productRepository = productRepository;
            _productCategoryRepository = productCategoryRepository;
        }

        public ActionResult Index(int? category,int page=1)
        {
            var pagePath = new List<string>();
            var products = _productRepository.GetList(false);
            if (!category.HasValue)
            {
                ViewBag.ColumnName = "全部产品";
            }
            else
            {
                var catList =
                    _productCategoryRepository.GetList(c => c.Id == category.Value || c.ParentId.Value == category.Value);
                var categoryDetail = catList.FirstOrDefault(c => c.Id == category.Value);
                if (categoryDetail!=null)
                {
                    if (categoryDetail.ParentId!=null && categoryDetail.ParentId!=0)
                    {
                        var topCategory =
                            _productCategoryRepository.GetList().FirstOrDefault(c => c.Id == categoryDetail.ParentId);
                        if (topCategory!=null)
                        {
                            pagePath.Add(topCategory.Name);
                        }
                    }
                    pagePath.Add(categoryDetail.Name);
                    
                    var catsId = catList.Select(c => c.Id).ToArray();
                    products = products.Where(c => catsId.Contains(c.CategoryId.Value));
                }
            }
            ViewBag.ColumnName = pagePath.ToArray();
            products = products.OrderByDescending(c => c.CreatedTime);
            const int pageSize = 6;
            var pros = products.ToPagedList(page, pageSize);
            var viewList = Mapper.Map<IEnumerable<Product>, IEnumerable<ProductViewModel>>(pros);
            var viewList2 = new StaticPagedList<ProductViewModel>(viewList, pros.PageNumber, pros.PageSize,
                                                                      pros.TotalItemCount);
            return View(viewList2);
        }

        [OutputCache(Duration = 30)]
        public ActionResult Category(string type)
        {
            var list = _productCategoryRepository.GetAll(showHidden: false).ToList();
            var vm = list.ToProductCategoryTree();
            if (type=="menu")
            {
                return View("Menu",vm);
            }
            return View(vm);
        }

        public ActionResult Detail(int id)
        {
            var product = _productRepository.GetById(id);
            var vm = Mapper.Map<Product, ProductDetailViewModel>(product);
            return this.View(vm);
        }

        public ActionResult RecommondProducts()
        {
            var recommondProducts = _productRepository.GetList(false).Where(c => c.IsRecommend).Take(10).ToList();
            var vm = Mapper.Map<List<Product>, List<ProductViewModel>>(recommondProducts);
            return View(vm);
        }

        public ActionResult Menu()
        {
            var categories = _productCategoryRepository.GetAll(showHidden: false).ToList();
            var vm = categories.ToProductCategoryTree();
            return View(vm);
        }
    }
}